﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace LynxWiki
{
    public static class WikiUtilities
    {
        public static object GetMySessionObject(this System.Web.Mvc.Controller cont, string sessionKeyItem)
        {
            return cont.HttpContext != null ? cont.HttpContext.Session[sessionKeyItem] : null;
        }

        public static MvcHtmlString TabbedMenu(this HtmlHelper helper, IEnumerable<MenuTab> tabs)
        {
            /*
             * @Html.TabbedMenu(new List<MenuTab> {
                    MenuTab.Create("Home", "Index", "Home"),
                    MenuTab.Create("About", "About", "Home"),
                    MenuTab.Create("Services", "Services", "Home")
                 })
             * 
             */
            var route = helper.ViewContext.RequestContext.RouteData;
            var controller = route.GetRequiredString("controller");
            var action = route.GetRequiredString("action");

            string menu = "\n\n<ul id=\"tabbedMenu\">";
            foreach (var tab in tabs) {
                if (controller == tab.Controller && action == tab.Action)
                    menu += "\n\t<li>" + helper.RouteLink(tab.Text, "PageDisplay", 
                        new { action = tab.Action, controller = tab.Controller,  pagePath = tab.Path }, new { @class = "selected" }) + "</li>";
                else if (tab.Text == "Topic" && controller == tab.Controller)
                    menu += "\n\t<li>" + helper.RouteLink(tab.Text, "PageDisplay",
                        new { action = tab.Action, controller = tab.Controller, pagePath = tab.Path }, new { @class = "selected" }) + "</li>";
                else if (tab.Text == "Discuss" && controller == tab.Controller)
                    menu += "\n\t<li>" + helper.RouteLink(tab.Text, "PageDisplay",
                        new { action = tab.Action, controller = tab.Controller, pagePath = tab.Path }, new { @class = "selected" }) + "</li>";
                else
                    menu += "\n\t<li>" + helper.RouteLink(tab.Text, "PageDisplay",
                   new { action = tab.Action, controller = tab.Controller, pagePath = tab.Path }, null) + "</li>";
            }
            menu += "\n</ul>\n\n";
            return MvcHtmlString.Create(menu);

        }


    }

    public static partial class HtmlHelperExtensions
    {
        public static string GetBrowserCssClass(this HtmlHelper htmlHelper)
        {
            string userAgent = htmlHelper.ViewContext.HttpContext.Request.UserAgent;
            if (userAgent != null) {
                if (userAgent.Contains("MSIE 8"))
                    return "IE IE8";

                if (userAgent.Contains("MSIE 7"))
                    return "IE IE7";

                if (userAgent.Contains("Chrome"))
                    return "Chrome";

                if (userAgent.Contains("Firefox/3"))
                    return "FF FF3";

                if (userAgent.Contains("Firefox"))
                    return "FF";

                if (userAgent.Contains("MSIE 6"))
                    return "IE IE6";

                if (userAgent.Contains("Safari"))
                    return "Safari";

                if (userAgent.Contains("Opera"))
                    return "Opera";
            }

            return "";
        }

    }

    public class MenuTab
    {
        private MenuTab(string text, string action, string path, string controller)
        {
            Text = text;
            Action = action;
            Path = path;
            Controller = controller;
        }

        public static MenuTab Create(string text, string action, string path, string controller)
        {
            return new MenuTab(text, action, path, controller);
        }

        public string Text { get; private set; }
        public string Action { get; private set; }
        public string Path { get; private set; }
        public string Controller { get; private set; }
    }

    public sealed class AcceptSubmitButtonAttribute : ActionMethodSelectorAttribute
    {
        private string _buttonName;
        private string _buttonValue;

        /// <summary>
        /// Holds the value of the Name attribute of the HTML submit button
        /// </summary>
        public string Name
        {
            get { return _buttonName; }
            set { _buttonName = value; }
        }

        /// <summary>
        /// Holds the name in the Value attribute of HTML submit button
        /// </summary>
        public string Value
        {
            get { return _buttonValue; }
            set { _buttonValue = value; }
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
        {
            return controllerContext.HttpContext.Request.Form[this._buttonName] == this._buttonValue;
        }
    }
}